package cz.cuni.amis.episodic.bayes.utils.chart;

import java.awt.Color;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.labels.XYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYStepAreaRenderer;
import org.jfree.chart.urls.StandardXYURLGenerator;
import org.jfree.chart.urls.XYURLGenerator;
import org.jfree.data.Range;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author ik
 */
public class AreaChart {

    public static JFreeChart chart(double[] entropy, Color color, Range yRange, String title) {
        XYSeries series = new XYSeries("Entropy");
        for (int i = 0; i < entropy.length; i++) {
            series.add(i-0.5, entropy[i]);
        }
        series.add(entropy.length - 0.5, 0);
        
        final XYSeriesCollection dataset = new XYSeriesCollection(series);
        
        final JFreeChart chart = createXYStepAreaChart(
            title,
            "Time", "Bits",
            dataset,
            PlotOrientation.VERTICAL,
            false,  // legend
            true,  // tool tips
            false,  // URLs
            yRange
        );
        
        // color
        final XYPlot plot = chart.getXYPlot();
        plot.getRenderer().setSeriesPaint(0, color);
        
        // fill shapes      
        final XYStepAreaRenderer rend = (XYStepAreaRenderer) plot.getRenderer();
        rend.setShapesFilled(true);

        return chart;
    }
    
      public static JFreeChart createXYStepAreaChart(String title,
                                                   String xAxisLabel,
                                                   String yAxisLabel,
                                                   XYDataset dataset,
                                                   PlotOrientation orientation,
                                                   boolean legend,
                                                   boolean tooltips,
                                                   boolean urls, 
                                                   Range yRange) {

        if (orientation == null) {
            throw new IllegalArgumentException("Null 'orientation' argument.");
        }
        NumberAxis xAxis = new NumberAxis(xAxisLabel);
        xAxis.setAutoRangeIncludesZero(false);
        // adjust numbers on X axis to natural numbers
        xAxis.setTickUnit(new NumberTickUnit(1));
        xAxis.setRange(-0.5, dataset.getItemCount(0)-0.5);

        NumberAxis yAxis = new NumberAxis(yAxisLabel);
        if(yRange != null) {
            yAxis.setRange(yRange);
        }
        XYToolTipGenerator toolTipGenerator = null;
        if (tooltips) {
            toolTipGenerator = new StandardXYToolTipGenerator();
        }

        XYURLGenerator urlGenerator = null;
        if (urls) {
            urlGenerator = new StandardXYURLGenerator();
        }
        XYItemRenderer renderer = new XYStepAreaRenderer(
                XYStepAreaRenderer.AREA, toolTipGenerator,
                urlGenerator);

        XYPlot plot = new XYPlot(dataset, xAxis, yAxis, null);
        plot.setRenderer(renderer);
        plot.setOrientation(orientation);
        plot.setDomainCrosshairVisible(false);
        plot.setRangeCrosshairVisible(false);
        JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
                plot, legend);
        chart.setBackgroundPaint(Color.WHITE);
        return chart;
    }
}
